var t = require("../../../@babel/runtime/helpers/interopRequireDefault"), a = require("../../../@babel/runtime/helpers/defineProperty"), e = t(require("../../../@babel/runtime/regenerator")), i = require("../../../@babel/runtime/helpers/asyncToGenerator"), n = getApp();

Page({
    data: {
        id: 0,
        show: !1,
        articleData: {},
        article: "",
        article_name: "",
        article_list: [],
        love_count: 0,
        love_member: [],
        base: {},
        isStart: !0,
        isEnd: !0,
        isActivated: !1,
        plugin: "message-base",
        catalogueProps: {},
        showLove: !0,
        showBase: !0
    },
    onLoad: function(t) {
        this.initOption();
    },
    initOption: function() {
        var t = this;
        return i(e.default.mark(function a() {
            var i, o;
            return e.default.wrap(function(a) {
                for (;;) switch (a.prev = a.next) {
                  case 0:
                    return i = new Map([ [ "message-base", {
                        index: "plugin.message-base.frontend.article.articleDetail",
                        baseArticle: "plugin.message-base.frontend.article.baseArticle",
                        params: function() {
                            return {
                                article_id: t.options.id,
                                visit_source: 2
                            };
                        },
                        baseArticleParams: {
                            base_id: t.options.bid
                        },
                        showLove: !0,
                        showBase: !0,
                        list: {
                            label: "article_list",
                            children: "article_name",
                            content: "article"
                        },
                        catalogueProps: {
                            label: "article_name",
                            children: "child"
                        },
                        callback: function(t) {},
                        getUD: function(a) {
                            t.setData({
                                isStart: a[0] && t.options.id == a[0].id,
                                isEnd: t.getIsEnd(a, t.options.id)
                            });
                        },
                        nextStartPage: function(a) {
                            if ("upPage" == a.type) return t.data.isStart ? n.tips("文章已经到顶了") : (t.upPage(t.data.article_list, t.data.article_list), 
                            !1);
                        },
                        nextEndPage: function(a) {
                            if ("nextPage" == a.type) {
                                if (t.data.isEnd) return n.tips("文章已经到底了");
                                for (var e = t.options.id, i = 0; i < t.data.article_list.length; i++) {
                                    var o = t.data.article_list[i];
                                    if (o.id == e) return o.child.length >= 1 ? (t.toDetail(o.child[0].id), !1) : !t.data.article_list[i + 1] || (t.toDetail(t.data.article_list[i + 1].id), 
                                    !1);
                                    if (t.nextPage(o.child, e)) return t.toDetail(t.data.article_list[i + 1].id), !1;
                                }
                            }
                        }
                    } ], [ "project-template", {
                        index: "plugin.project-template.frontend.knowledge.articleDetatil",
                        baseArticle: "plugin.project-template.frontend.knowledge.knowledgeBaseCatalog",
                        params: function() {
                            return {
                                id: t.options.id
                            };
                        },
                        baseArticleParams: {
                            id: t.options.bid
                        },
                        showLove: !1,
                        showBase: !1,
                        list: {
                            label: "knowledgeBaseArticle",
                            children: "title",
                            content: "content"
                        },
                        catalogueProps: {
                            label: "title",
                            children: "has_many_yz_supply_knowledge_base_article"
                        },
                        callback: function(t) {},
                        getUD: function() {
                            t.setData({
                                isStart: !t.data.articleData.previous_id,
                                isEnd: !t.data.articleData.next_id
                            });
                        },
                        nextStartPage: function(a) {
                            if ("upPage" == a.type) return t.data.articleData.previous_id ? (t.toDetail(t.data.articleData.previous_id), 
                            !1) : n.tips("文章已经到顶了");
                        },
                        nextEndPage: function(a) {
                            if ("nextPage" == a.type) {
                                if (!t.data.articleData.next_id) return n.tips("文章已经到底了");
                                t.toDetail(t.data.articleData.next_id);
                            }
                        }
                    } ] ]), t.options.plu && (t.data.plugin = t.options.plu), o = i.get(t.data.plugin), 
                    t.setData({
                        apiOption: o,
                        catalogueProps: o.catalogueProps,
                        showLove: o.showLove,
                        showBase: o.showBase,
                        isActivated: !0,
                        id: t.options.id
                    }), a.next = 6, t.getdata();

                  case 6:
                    t.getarticle_list();

                  case 7:
                  case "end":
                    return a.stop();
                }
            }, a);
        }))();
    },
    getIsStart: function(t) {
        this.data.apiOption.getUD(t);
    },
    getIsEnd: function(t, a) {
        if (t && t[t.length - 1]) {
            var e = t[t.length - 1].child;
            return e && e.length > 0 ? this.getIsEnd(e, a) : t[t.length - 1].id == a;
        }
        return !0;
    },
    treeTap: function(t) {
        var a = t.detail.item.id;
        wx.redirectTo({
            url: "/packageI/knowledge/knowledgeDetail/knowledgeDetail?id=".concat(a, "&bid=").concat(this.options.bid, "&plu=").concat(this.data.plugin)
        });
    },
    toDetail: function(t) {
        var a = this.options.bid;
        wx.redirectTo({
            url: "/packageI/knowledge/knowledgeDetail/knowledgeDetail?id=".concat(t, "&bid=").concat(a, "&plu=").concat(this.data.plugin)
        });
    },
    nextPage: function(t, a) {
        for (var e = 0; e < t.length; e++) {
            var i = t[e];
            if (i.id == a) return i.child.length >= 1 ? (this.toDetail(i.child[0].id), !1) : !t[e + 1] || (this.toDetail(t[e + 1].id), 
            !1);
            if (this.nextPage(i.child, a)) return !t[e + 1] || (this.toDetail(t[e + 1].id), 
            !1);
        }
    },
    upPage: function(t, a, e) {
        for (var i = this.options.id, n = 0; n < t.length; n++) {
            var o = t[n];
            if (o.id == i) {
                var r = n <= 0 ? a[e].id : t[n - 1].id;
                return this.toDetail(r), !1;
            }
            this.upPage(o.child, t, n);
        }
    },
    operationPage: function(t) {
        var a = t.target.dataset.type || t.currentTarget.dataset.type;
        this.data.article_list && this.data.article_list.length > 0 && ("upPage" == a && this.data.apiOption.nextStartPage({
            type: a
        }), "nextPage" == a && this.data.apiOption.nextEndPage({
            type: a
        }));
    },
    getdata: function() {
        var t = this;
        return i(e.default.mark(function a() {
            var i;
            return e.default.wrap(function(a) {
                for (;;) switch (a.prev = a.next) {
                  case 0:
                    if (!t.options.id || -1 != t.options.id) {
                        a.next = 3;
                        break;
                    }
                    return wx.redirectTo({
                        url: "/packageI/knowledge/knowledgeList/knowledgeList?bid=".concat(t.options.bid, "&plu=").concat(t.data.plugin)
                    }), a.abrupt("return");

                  case 3:
                    i = n.getNetAddresss(t.data.apiOption.index), n._postNetWork({
                        url: i,
                        data: t.data.apiOption.params(),
                        success: function(a) {
                            var e = a.data;
                            if (1 == e.result) {
                                if (e.data.article) {
                                    try {
                                        t.data.article = decodeURIComponent(e.data.article[t.data.apiOption.list.content]);
                                    } catch (a) {
                                        t.data.article = e.data.article[t.data.apiOption.list.content];
                                    }
                                    t.data.article_name = e.data.article[t.data.apiOption.list.children], wx.setNavigationBarTitle({
                                        title: t.data.article_name || "知识库详情"
                                    });
                                }
                                t.setData({
                                    article: t.data.article,
                                    articleData: e.data,
                                    love_count: e.data.love_count,
                                    love_member: e.data.love_member,
                                    base: e.data.base || {}
                                });
                            } else wx.showToast({
                                icon: "none",
                                title: e.msg,
                                duration: 1e3
                            });
                        },
                        fail: function(t) {
                            console.log(t);
                        }
                    });

                  case 5:
                  case "end":
                    return a.stop();
                }
            }, a);
        }))();
    },
    getarticle_list: function() {
        this.showtime({
            target: {
                dataset: {
                    show: !1
                }
            }
        });
    },
    showtime: function(t) {
        var a = this, e = !1;
        try {
            e = t.target.dataset.show || t.currentTarget.dataset.show;
        } catch (t) {}
        if (this.data.article_list && this.data.article_list.length > 0) return this.setData({
            show: e
        }), !1;
        var i = n.getNetAddresss(this.data.apiOption.baseArticle);
        n._postNetWork({
            url: i,
            data: this.data.apiOption.baseArticleParams,
            success: function(t) {
                var i = t.data;
                1 == i.result ? (a.data.article_list = i.data[a.data.apiOption.list.label].filter(function(t) {
                    return "" != t[a.data.apiOption.list.children];
                }), a.setData({
                    article_list: a.data.article_list
                }), a.getIsStart(a.data.article_list), a.setData({
                    show: e
                })) : wx.showToast({
                    icon: "none",
                    title: i.msg,
                    duration: 1e3
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    clicklove: function() {
        var t = this, e = n.getNetAddresss("plugin.message-base.frontend.article.articleLove");
        n._postNetWork({
            url: e,
            data: {
                article_id: this.options.id,
                is_love: this.data.articleData.is_love ? 0 : 1
            },
            success: function(e) {
                var i, n = e.data;
                (wx.showToast({
                    icon: "none",
                    title: n.msg,
                    duration: 1e3
                }), 1 == n.result) && (t.setData((a(i = {}, "articleData.is_love", !t.data.articleData.is_love), 
                a(i, "love_count", t.data.articleData.is_love ? t.data.love_count++ : t.data.love_count--), 
                i)), t.getdata());
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    toBack: function() {
        var t = this;
        wx.navigateBack({
            delta: 1,
            fail: function() {
                t.toHome();
            }
        });
    },
    toHome: function() {
        wx.redirectTo({
            url: "/packageI/knowledge/knowledgeIndex/knowledgeIndex?plu=".concat(this.data.plugin)
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {
        this.setData({
            isActivated: !1
        });
    },
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        var t = wx.getStorageSync("yz_uid"), a = "";
        return t && (a = t), {
            path: "/packageI/knowledge/knowledgeDetail/knowledgeDetail?plu=" + this.options.plu + "&bid=" + this.options.bid + "&id=" + this.options.id + "&mid=" + a
        };
    }
});